
// ChemGraphicsDoc.cpp : implementation of the CChemGraphicsDoc class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "ChemGraphics.h"
#endif

#include "ChemGraphicsDoc.h"
#include "ChemGraphicsView.h"
#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CChemGraphicsDoc

IMPLEMENT_DYNCREATE(CChemGraphicsDoc, CDocument)

//28/09/2014 - tracker
BEGIN_MESSAGE_MAP(CChemGraphicsDoc, CDocument)
	//{{AFX_MSG_MAP(CTrackerDoc)
	ON_COMMAND(ID_EDIT_DOTTEDLINE, OnEditDottedline)
	ON_UPDATE_COMMAND_UI(ID_EDIT_DOTTEDLINE, OnUpdateEditDottedline)
	ON_COMMAND(ID_EDIT_HATCHEDBORDER, OnEditHatchedborder)
	ON_UPDATE_COMMAND_UI(ID_EDIT_HATCHEDBORDER, OnUpdateEditHatchedborder)
	ON_UPDATE_COMMAND_UI(ID_EDIT_HATCHEDINSIDE, OnUpdateEditHatchedinside)
	ON_COMMAND(ID_EDIT_HATCHEDINSIDE, OnEditHatchedinside)
	ON_COMMAND(ID_EDIT_RESIZEINSIDE, OnEditResizeinside)
	ON_UPDATE_COMMAND_UI(ID_EDIT_RESIZEINSIDE, OnUpdateEditResizeinside)
	ON_COMMAND(ID_EDIT_RESIZEOUTSIDE, OnEditResizeoutside)
	ON_UPDATE_COMMAND_UI(ID_EDIT_RESIZEOUTSIDE, OnUpdateEditResizeoutside)
	ON_COMMAND(ID_EDIT_SOLIDLINE, OnEditSolidline)
	ON_UPDATE_COMMAND_UI(ID_EDIT_SOLIDLINE, OnUpdateEditSolidline)
	ON_COMMAND(ID_EDIT_ALLOWINVERT, OnEditAllowInvert)
	ON_UPDATE_COMMAND_UI(ID_EDIT_ALLOWINVERT, OnUpdateEditAllowInvert)
	//}}AFX_MSG_MAP
	//{{SHAPE_MSG_MAP
	ON_COMMAND(ID_ARROW, OnDrawArrow)
	ON_COMMAND(ID_LINE, OnDrawLine)
	ON_COMMAND(ID_SQUARE, OnDrawSquare)
	ON_COMMAND(ID_RECTANGLE1, OnDrawRectangle)
	ON_COMMAND(ID_CIRCLE, OnDrawCircle)
	ON_COMMAND(ID_ECLIPSE, OnDrawEclipse)
	//}}SHAPE_MSG_MAP
	//{{BOND_MSG_MAP
	ON_COMMAND(ID_ONEBOND, OnDrawOneBond)
	ON_COMMAND(ID_DOUBLEBOND, OnDrawDoubleBond)
	ON_COMMAND(ID_THREEBONE, OnDrawThreeBond)
	ON_COMMAND(ID_CYCLOHEXANE, OnDrawCycloHexane)
	ON_COMMAND(ID_CIRCLEHEXANE, OnDrawCircleHexane)
	ON_COMMAND(ID_BENZENE, OnDrawBenzene)
	ON_COMMAND(ID_CIRCLEPENTANE, OnDrawCirclePentane)
	ON_COMMAND(ID_WEDGEBOND, OnDrawWedgeBond)
	ON_COMMAND(ID_HASHEDWAGED, OnDrawHashedWedge)
	ON_COMMAND(ID_HASHBOND, OnDrawHashBond)
	ON_COMMAND(ID_WHITEWEDGEBOND, OnDrawWhiteHashBond)
	ON_COMMAND(ID_UNDEFINEBOND, OnDrawUndefineBond)
	ON_COMMAND(ID_BOLDBOND, OnDrawBoldBond)
	ON_COMMAND(ID_DOUBLEBOLDBOND, OnDrawDoubleBoldBond)
	ON_COMMAND(ID_DOUBLEARROW, OnDrawDoubleArrow)
	//}}BOND_MSG_MAP
	//{{OBITAN_MSG_MAP
	ON_COMMAND(ID_CIRCLEOBITAN, OnDrawCircleObitan)
	ON_COMMAND(ID_BCIRCLEOBITAN, OnDrawBCircleObitan)
	ON_COMMAND(ID_ECLIPSEOBITAN, OnDrawEclipseObitan)
	ON_COMMAND(ID_BECLIPSEOBITAN, OnDrawBEclipseObitan)
	ON_COMMAND(ID_FUNNELOBITAN, OnDrawFunnelObitan)
	ON_COMMAND(ID_BFUNNELOBITAN, OnDrawBFunnelObitan)
	ON_COMMAND(ID_DFUNNELOBITAN, OnDrawDFunnelObitan)
	ON_COMMAND(ID_DBFUNNELOBITAN, OnDrawDBFunnelObitan)
	ON_COMMAND(ID_DFUNNELOBITAN1, OnDrawDFunnelObitan1)
	ON_COMMAND(ID_DBFUNNELOBITAN1, OnDrawDBFunnelObitan1)
	ON_COMMAND(ID_DBFUNNELOBITAN2, OnDrawDBFunnelObitan2)
	//}}OBITAN_MSG_MAP
	//{{SYMBOL_MSG_MAP
	ON_COMMAND(ID_PLUSCIRCLE, OnDrawPlusCircle)
	ON_COMMAND(ID_SUBCIRCLE, OnDrawSubCircle)
	ON_COMMAND(ID_PLUS, OnDrawPlus)
	ON_COMMAND(ID_SUB, OnDrawSub)
	ON_COMMAND(ID_PLUSPOINT, OnDrawPlusPoint)
	ON_COMMAND(ID_SUBPOINT, OnDrawSubPoint)
	ON_COMMAND(ID_ONEPOINT, OnDrawOnePoint)
	ON_COMMAND(ID_DOUBLEPOINT, OnDrawDoublePoint)
	//}}SYMBOL_MSG_MAP
	ON_COMMAND(ID_DIALOG_FONT, &CChemGraphicsDoc::OnDialogFont)
	ON_UPDATE_COMMAND_UI(ID_DIALOG_FONT, &CChemGraphicsDoc::OnUpdateDialogFont)
	ON_COMMAND(ID_EDIT_PASTE, &CChemGraphicsDoc::OnEditPaste)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, &CChemGraphicsDoc::OnUpdateEditPaste)
	ON_COMMAND(ID_TEXT, &CChemGraphicsDoc::OnText)
	ON_UPDATE_COMMAND_UI(ID_ARROW, &CChemGraphicsDoc::OnUpdateArrow)
	ON_UPDATE_COMMAND_UI(ID_CIRCLE, &CChemGraphicsDoc::OnUpdateCircle)
	ON_UPDATE_COMMAND_UI(ID_ECLIPSE, &CChemGraphicsDoc::OnUpdateEclipse)
	ON_UPDATE_COMMAND_UI(ID_LINE, &CChemGraphicsDoc::OnUpdateLine)
	ON_UPDATE_COMMAND_UI(ID_RECTANGLE1, &CChemGraphicsDoc::OnUpdateRectangle1)
	ON_UPDATE_COMMAND_UI(ID_SQUARE, &CChemGraphicsDoc::OnUpdateSquare)
	ON_UPDATE_COMMAND_UI(ID_ONEBOND, &CChemGraphicsDoc::OnUpdateOnebond)
	ON_UPDATE_COMMAND_UI(ID_DOUBLEBOND, &CChemGraphicsDoc::OnUpdateDoublebond)
	ON_UPDATE_COMMAND_UI(ID_THREEBONE, &CChemGraphicsDoc::OnUpdateThreebone)
	ON_UPDATE_COMMAND_UI(ID_CYCLOHEXANE, &CChemGraphicsDoc::OnUpdateCyclohexane)
	ON_UPDATE_COMMAND_UI(ID_CIRCLEHEXANE, &CChemGraphicsDoc::OnUpdateCirclehexane)
	ON_UPDATE_COMMAND_UI(ID_BENZENE, &CChemGraphicsDoc::OnUpdateBenzene)
	ON_UPDATE_COMMAND_UI(ID_CIRCLEPENTANE, &CChemGraphicsDoc::OnUpdateCirclepentane)
	ON_UPDATE_COMMAND_UI(ID_WEDGEBOND, &CChemGraphicsDoc::OnUpdateWedgebond)
	ON_UPDATE_COMMAND_UI(ID_HASHEDWAGED, &CChemGraphicsDoc::OnUpdateHashedwaged)
	ON_UPDATE_COMMAND_UI(ID_HASHBOND, &CChemGraphicsDoc::OnUpdateHashbond)
	ON_UPDATE_COMMAND_UI(ID_WHITEWEDGEBOND, &CChemGraphicsDoc::OnUpdateWhitewedgebond)
	ON_UPDATE_COMMAND_UI(ID_UNDEFINEBOND, &CChemGraphicsDoc::OnUpdateUndefinebond)
	ON_UPDATE_COMMAND_UI(ID_BOLDBOND, &CChemGraphicsDoc::OnUpdateBoldbond)
	ON_UPDATE_COMMAND_UI(ID_DOUBLEBOLDBOND, &CChemGraphicsDoc::OnUpdateDoubleboldbond)
	ON_UPDATE_COMMAND_UI(ID_DOUBLEARROW, &CChemGraphicsDoc::OnUpdateDoublearrow)
	ON_UPDATE_COMMAND_UI(ID_BCIRCLEOBITAN, &CChemGraphicsDoc::OnUpdateBcircleobitan)
	ON_UPDATE_COMMAND_UI(ID_ECLIPSEOBITAN, &CChemGraphicsDoc::OnUpdateEclipseobitan)
	ON_UPDATE_COMMAND_UI(ID_BECLIPSEOBITAN, &CChemGraphicsDoc::OnUpdateBeclipseobitan)
	ON_UPDATE_COMMAND_UI(ID_FUNNELOBITAN, &CChemGraphicsDoc::OnUpdateFunnelobitan)
	ON_UPDATE_COMMAND_UI(ID_BFUNNELOBITAN, &CChemGraphicsDoc::OnUpdateBfunnelobitan)
	ON_UPDATE_COMMAND_UI(ID_DFUNNELOBITAN, &CChemGraphicsDoc::OnUpdateDfunnelobitan)
	ON_UPDATE_COMMAND_UI(ID_DBFUNNELOBITAN, &CChemGraphicsDoc::OnUpdateDbfunnelobitan)
	ON_UPDATE_COMMAND_UI(ID_DFUNNELOBITAN1, &CChemGraphicsDoc::OnUpdateDfunnelobitan1)
	ON_UPDATE_COMMAND_UI(ID_DBFUNNELOBITAN1, &CChemGraphicsDoc::OnUpdateDbfunnelobitan1)
	ON_UPDATE_COMMAND_UI(ID_DBFUNNELOBITAN2, &CChemGraphicsDoc::OnUpdateDbfunnelobitan2)
	ON_UPDATE_COMMAND_UI(ID_PLUSCIRCLE, &CChemGraphicsDoc::OnUpdatePluscircle)
	ON_UPDATE_COMMAND_UI(ID_SUBCIRCLE, &CChemGraphicsDoc::OnUpdateSubcircle)
	ON_UPDATE_COMMAND_UI(ID_PLUS, &CChemGraphicsDoc::OnUpdatePlus)
	ON_UPDATE_COMMAND_UI(ID_PLUSPOINT, &CChemGraphicsDoc::OnUpdatePluspoint)
	ON_UPDATE_COMMAND_UI(ID_SUBPOINT, &CChemGraphicsDoc::OnUpdateSubpoint)
	ON_UPDATE_COMMAND_UI(ID_ONEPOINT, &CChemGraphicsDoc::OnUpdateOnepoint)
	ON_UPDATE_COMMAND_UI(ID_DOUBLEPOINT, &CChemGraphicsDoc::OnUpdateDoublepoint)
	ON_UPDATE_COMMAND_UI(ID_SUB, &CChemGraphicsDoc::OnUpdateSub)
	ON_UPDATE_COMMAND_UI(ID_CIRCLEOBITAN, &CChemGraphicsDoc::OnUpdateCircleobitan)
	ON_COMMAND(ID_CYCLOPROPANE, &CChemGraphicsDoc::OnCyclopropane)
	ON_UPDATE_COMMAND_UI(ID_CYCLOPROPANE, &CChemGraphicsDoc::OnUpdateCyclopropane)
	ON_COMMAND(ID_CYLOPUTAN, &CChemGraphicsDoc::OnCyloputan)
	ON_UPDATE_COMMAND_UI(ID_CYLOPUTAN, &CChemGraphicsDoc::OnUpdateCyloputan)
	ON_COMMAND(ID_BUTTON_SELECTAREA, &CChemGraphicsDoc::OnButtonSelectarea)
	END_MESSAGE_MAP()

CChemGraphicsView *g_ptrView;

// CChemGraphicsDoc construction/destruction

CChemGraphicsDoc::CChemGraphicsDoc()
{
	// TODO: add one-time construction code here
	//28/09/2014 - tracker
	m_bAllowInvert = FALSE;
	m_CurrText = -1;
	m_szText= "";
	m_nCaretIndex=0;
	m_nShapeCurrent = -1;
	m_nRotate = 0;
	m_bUndo = FALSE;
//	m_nShapeType = SELECT_T;

}

CChemGraphicsDoc::~CChemGraphicsDoc()
{
}

BOOL CChemGraphicsDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	//text
	CClientDC dc(NULL);
	LOGFONT lf; 
	CFont *ptrFont;
	TEXTMETRIC tm;
	ptrFont = dc.GetCurrentFont(); 
	ptrFont->GetLogFont(&lf);
	//VERIFY(m_ftDraw.CreateFontIndirect(&lf));
	dc.GetOutputTextMetrics(&tm); 
	m_nCaretVerSize = tm.tmHeight;
	//text
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	m_clrBack = RGB(255,255,255);
	m_cPenColor = RGB(0,0,0);
	m_arrShape.RemoveAll();
	return TRUE;
}




// CChemGraphicsDoc serialization

void CChemGraphicsDoc::Serialize(CArchive& ar)
{
	int i, ncount;
	BaseShape *pShape = NULL;
	if (ar.IsStoring())
	{
		//28/09/2014 - tracker
		// TODO: add storing code here
/*		DWORD dw = m_tracker.m_nStyle;
		WORD w = (WORD)m_bAllowInvert;
		ar << m_tracker.m_rect << dw << w;
*/
		ncount = m_arrShape.GetSize();
		ar << m_arrShape.GetSize();
		for ( i = 0; i < ncount; i++ )
		{
			pShape = m_arrShape.GetAt(i);
			pShape->Serialize ( ar);
		}
	}
	else
	{
		//28/09/2014 - tracker
		// TODO: add loading code here
/*		DWORD dw;
		WORD w;
		ar >> m_tracker.m_rect >> dw >> w;
		m_tracker.m_nStyle = (UINT)dw;
		m_bAllowInvert = w;
*/
		BaseShape *CShape = NULL;
		pShape = new BaseShape;
		ar >> ncount;
		m_arrShape.RemoveAll();
		m_arrShapeDelete.RemoveAll();
		for ( i = 0; i < ncount; i++ )
		{
			pShape->Serialize ( ar );
			switch (pShape->m_nShapeType)
			{
				case TEXT_T:
					CShape = new CText (pShape->m_pBegin, pShape->m_pEnd, pShape->m_nShapeType, pShape->m_szText);
					break;
				case CYCLOPROPANE_T:
					CShape = new CCycloPropane (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case CYCLOPUTANE_T:
					CShape = new CCycloPutane (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case SQUARE_T:
					CShape = new CSquare (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case CIRCLE_T:
					CShape = new CCircle (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case RECTANEGLE_T:
					CShape = new CRectangle (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case LINE_T:
					CShape = new CLine (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case ECLIPSE_T:
					CShape = new CEclipse (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case ARROW_T:
					CShape = new CEclipse (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case TRIBOND_T:
					CShape = new CTriBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case DOUBLEBOLDBOND_T:
					CShape = new CDoubleBoldBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case DOUBLEBOND_T:
					CShape = new CDoubleBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case ONEBOND_T:
					CShape = new COneBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case BENZENBOND_T:
					CShape = new CBenzenBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case CYCLOHEXANEBOND_T:
					CShape = new CCycloHexaneBone (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case CIRCLEBENZENE_T:
					CShape = new CCircleBenzene (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case CYCLOPENTANE_T:
					CShape = new CCycloPentane (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case HASHEDWEDGE_T:
					CShape = new CHasheWedge (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case HASHBOND_T:
					CShape = new CHashBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case WHITEHASHBOND_T:
					CShape = new CWhiteHashBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case UNDEFINEBOND_T:
					CShape = new CUndefineBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case BOLDBOND_T:
					CShape = new CBoldBond (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case DOUBLEARROW_T:
					CShape = new CDoubleArrow (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				// symbol class
				case PLUSCIRCLE_T:
					CShape = new CPlusCircle ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case PLUSPOINT_T:
					CShape = new CPlusPoint ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case PLUS_T:
					CShape = new CPlus ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case SUBCIRCLE_T:
					CShape = new CSubCircle ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case SUBPOINT_T:
					CShape = new CSubPoint ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case SUB_T:
					CShape = new CSub ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case ONEPOINT_T:
					CShape = new COnePoint ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				case DOUBLEPOINT_T:
					CShape = new CDoublePoint ( pShape->m_pBegin, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType);
					break;
				// obital
				case OBITALBOLD_T:
					CShape = new CObitalBold (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALCIRCLE_T:
					CShape = new CObitalCircle (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALPIE_T:
					CShape = new CObitalPie (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALESCLIPSE_T:
					CShape = new CObitalEsclipe (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALESCLIPSEBOLD_T:
					CShape = new CObitalEsclipeBold (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALDOUBLEPIE_T:
					CShape = new CObitalFlower (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALDOUBLEPIEBOLD_T:
					CShape = new CObitalFlowerBold (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALSMFLOWER_T:
					CShape = new CObitalSmallFlower (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				case OBITALSMFLOWERBOLD_T:
					CShape = new CObitalBSmallFlower (pShape->m_pBegin, pShape->m_pEnd, pShape->m_clrBack, pShape->m_cPenColor, pShape->m_nShapeType, pShape->m_nRotate);
					break;
				default:
					break;
			}
			m_arrShape.Add (CShape);
		}
	}
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CChemGraphicsDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CChemGraphicsDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CChemGraphicsDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CChemGraphicsDoc diagnostics

#ifdef _DEBUG
void CChemGraphicsDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CChemGraphicsDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CChemGraphicsDoc commands
void CChemGraphicsDoc::OnEditSolidline()
{
	CRect rectTrue;
	m_tracker.GetTrueRect(&rectTrue);
	m_tracker.m_nStyle &= ~CRectTracker::dottedLine;
	m_tracker.m_nStyle ^= CRectTracker::solidLine;
	SetModifiedFlag();
	UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectTrue);
	UpdateAllViews(NULL);
}

void CChemGraphicsDoc::OnUpdateEditSolidline(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(!!(m_tracker.m_nStyle & CRectTracker::solidLine));
}

void CChemGraphicsDoc::OnEditDottedline()
{
	CRect rectTrue;
	m_tracker.GetTrueRect(&rectTrue);
	m_tracker.m_nStyle &= ~CRectTracker::solidLine;
	m_tracker.m_nStyle ^= CRectTracker::dottedLine;
	SetModifiedFlag();
	UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectTrue);
	UpdateAllViews(NULL);
}

void CChemGraphicsDoc::OnUpdateEditDottedline(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(!!(m_tracker.m_nStyle & CRectTracker::dottedLine));
}

void CChemGraphicsDoc::OnEditHatchedborder()
{
	CRect rectTrue;
	m_tracker.GetTrueRect(&rectTrue);
	m_tracker.m_nStyle ^= CRectTracker::hatchedBorder;
	SetModifiedFlag();
	UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectTrue);
	UpdateAllViews(NULL);
}

void CChemGraphicsDoc::OnUpdateEditHatchedborder(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(!!(m_tracker.m_nStyle & CRectTracker::hatchedBorder));
}

void CChemGraphicsDoc::OnEditHatchedinside()
{
	CRect rectTrue;
	m_tracker.GetTrueRect(&rectTrue);
	m_tracker.m_nStyle ^= CRectTracker::hatchInside;
	SetModifiedFlag();
	UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectTrue);
	UpdateAllViews(NULL);
}

void CChemGraphicsDoc::OnUpdateEditHatchedinside(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(!!(m_tracker.m_nStyle & CRectTracker::hatchInside));
}

void CChemGraphicsDoc::OnEditResizeinside()
{
	CRect rectTrue;
	m_tracker.GetTrueRect(&rectTrue);
	m_tracker.m_nStyle &= ~CRectTracker::resizeOutside;
	m_tracker.m_nStyle ^= CRectTracker::resizeInside;
	SetModifiedFlag();
	UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectTrue);
	UpdateAllViews(NULL);
}

void CChemGraphicsDoc::OnUpdateEditResizeinside(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(!!(m_tracker.m_nStyle & CRectTracker::resizeInside));
}

void CChemGraphicsDoc::OnEditResizeoutside()
{
	CRect rectTrue;
	m_tracker.GetTrueRect(&rectTrue);
	m_tracker.m_nStyle &= ~CRectTracker::resizeInside;
	m_tracker.m_nStyle ^= CRectTracker::resizeOutside;
	SetModifiedFlag();
	UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectTrue);
	UpdateAllViews(NULL);
}

void CChemGraphicsDoc::OnUpdateEditResizeoutside(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(!!(m_tracker.m_nStyle & CRectTracker::resizeOutside));
}

void CChemGraphicsDoc::OnEditAllowInvert()
{	
	m_nShapeType = SELECT_T;
	m_bAllowInvert = !m_bAllowInvert;
	SetModifiedFlag();
}

void CChemGraphicsDoc::OnUpdateEditAllowInvert(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_bAllowInvert);
}
//01-10-2014 - Ham tra ve vi tri hinh dc con tro chuot click vao
void CChemGraphicsDoc::OnToolsOptions()
{
	
}

INT CChemGraphicsDoc::isPointShape(CPoint m_ptMouse)
{
	int i, j, m_nAngle, m_nNumOfShape, k = -1;
	m_nNumOfShape = (int)m_arrShape.GetSize();
	if ( m_nNumOfShape > 0)
		for (int m_nCount = m_nNumOfShape - 1; m_nCount >= 0 ; m_nCount--)
		{
			m_nAngle = m_arrShape[m_nCount] -> m_nAngle;
			bool m_bFlag = false;
			for(i = 0, j = m_nAngle - 1; i < m_nAngle; j = i++) 
			{
				if ( ( ( m_arrShape[m_nCount]->m_pCoordinates[i].y >= m_ptMouse.y )  != ( m_arrShape[m_nCount]->m_pCoordinates[j].y >= m_ptMouse.y ) )  &&
					(m_ptMouse.x <= (m_arrShape[m_nCount]->m_pCoordinates[j].x - m_arrShape[m_nCount]->m_pCoordinates[i].x) 
					* (m_ptMouse.y - m_arrShape[m_nCount]->m_pCoordinates[i].y) / (m_arrShape[m_nCount]->m_pCoordinates[j].y 
					- m_arrShape[m_nCount]->m_pCoordinates[i].y) + m_arrShape[m_nCount]->m_pCoordinates[i].x) )
				m_bFlag = !m_bFlag;
			}
			if (m_bFlag)
			{
				k = m_nCount;
				break;
			}
		}
	return k;
}

//09-10-2014 tra ve vi tri con tro chuot
void CChemGraphicsDoc::LineEquation2(INT &A, INT &B, INT &C, int m_nCount)
{
	A = m_arrShape[m_nCount]->m_pEnd.y - m_arrShape[m_nCount]->m_pBegin.y;
	B = m_arrShape[m_nCount]->m_pBegin.x - m_arrShape[m_nCount]->m_pEnd.x;
	C = m_arrShape[m_nCount]->m_pBegin.y * m_arrShape[m_nCount]->m_pEnd.x - m_arrShape[m_nCount]->m_pBegin.x * m_arrShape[m_nCount]->m_pEnd.y;
}

INT CChemGraphicsDoc::isPointShape2(CPoint m_ptMouse)
{
	INT A, B, C; //he so cua phuong trinh duong thang
	INT m_nNumOfShape, k = -1;
	DOUBLE m_dLineDist;
	const int constDist = 5;
	int x1, y1, x2, y2;
	m_nNumOfShape = (int)m_arrShape.GetSize();
	if ( m_nNumOfShape > 0)
	{
		for (int m_nCount = m_nNumOfShape - 1; m_nCount >= 0 ; m_nCount--)
			if( m_arrShape[m_nCount]->m_nShapeType == LINE_T)
			{
				//Tao he so cho phuong trinh duong thang
				this->LineEquation2(A, B, C, m_nCount);
				//Tinh khoang cach tu diem toi phuong trinh duong thang
				m_dLineDist = abs(A * m_ptMouse.x + B * m_ptMouse.y + C) / sqrt(A * A + B * B);

				//kiem tra diem thuoc duong thang
				x2 = max(m_arrShape[m_nCount]->m_pBegin.x, m_arrShape[m_nCount]->m_pEnd.x) + constDist;
				x1 = min(m_arrShape[m_nCount]->m_pBegin.x, m_arrShape[m_nCount]->m_pEnd.x);
				y2 = max(m_arrShape[m_nCount]->m_pBegin.y, m_arrShape[m_nCount]->m_pEnd.y) + constDist;
				y1 = min(m_arrShape[m_nCount]->m_pBegin.y, m_arrShape[m_nCount]->m_pEnd.y);
				if((m_ptMouse.x <= x2) && (m_ptMouse.x >= x1) && (m_ptMouse.y <= y2) && (m_ptMouse.y >= y1) && (m_dLineDist <= constDist) )
				{	
					k = m_nCount;
					break;
				}
			}
	}
	return k;
}

//11-10-2014
INT CChemGraphicsDoc::isPointShape3(CPoint m_ptMouse)
{
	INT A, B, C; //he so cua phuong trinh duong thang
	DOUBLE m_dLineDist;
	float CDist, LDist, RDist;
	const int constDist = 5;
	INT m_nNumOfShape = (int)m_arrShape.GetSize(), k = -1;//k: ket qua tra ve vi tri hien tai
	if ( m_nNumOfShape > 0)
	{
		for (int m_nCount = m_nNumOfShape - 1; m_nCount >= 0 ; m_nCount--)
		{
			if( m_arrShape[m_nCount]->m_nShapeType == CYCLOHEXANEBOND_T)
			for(int i = 0; i < m_arrShape[m_nCount]->m_nAngle; i++)
			{
				this->LineEquation3(A, B, C, m_nCount, i);

				//tinh khoang cach tu diem toi duong thang
				m_dLineDist = abs(A * m_ptMouse.x + B * m_ptMouse.y + C) / sqrt(A * A + B * B);
				if(this->Between(m_nCount, m_ptMouse, i) == TRUE && m_dLineDist <= constDist)
				{
					
					this->LineDist(CDist, LDist, RDist, m_nCount, m_ptMouse, i);
					if(LDist < CDist)
						m_arrShape[m_nCount]->m_nViTri = 1;
					else if(RDist < CDist)
						m_arrShape[m_nCount]->m_nViTri = -1;
					else m_arrShape[m_nCount]->m_nViTri = 0;
					m_arrShape[m_nCount]->m_nCanh = i;
	
					k = m_nCount;
					break;
				}
			}
			if(k > -1) break;
		}
	}
	return k;
}
/*
void CChemGraphicsDoc::getCenter(CPoint &m_pCenter, INT m_nCurrentShape, int i)
{
	m_pCenter = ((m_arrShape[m_nCurrentShape]->m_pCoordinates[i].x + m_arrShape[m_nCurrentShape]->m_pCoordinates[i].x) / 2, 
				 (m_arrShape[m_nCurrentShape]->m_pCoordinates[i + 1].y + m_arrShape[m_nCurrentShape]->m_pCoordinates[i + 1].y) / 2);
	if(i == m_arrShape[m_nCurrentShape]->m_nAngle - 1)
		m_pCenter = ((m_arrShape[m_nCurrentShape]->m_pCoordinates[i].x + m_arrShape[m_nCurrentShape]->m_pCoordinates[i].x) / 2, 
					(m_arrShape[m_nCurrentShape]->m_pCoordinates[0].y + m_arrShape[m_nCurrentShape]->m_pCoordinates[0].y) / 2);

}
*/
void CChemGraphicsDoc::LineEquation3(INT &A, INT &B, INT &C, INT m_nCurrentShape, int i)
{
	int b = i,e = i + 1 ;
	if(i == m_arrShape[m_nCurrentShape]->m_nAngle - 1)
		e = 0;
	
	A = m_arrShape[m_nCurrentShape]->m_pCoordinates[e].y - m_arrShape[m_nCurrentShape]->m_pCoordinates[b].y;
	B = m_arrShape[m_nCurrentShape]->m_pCoordinates[b].x - m_arrShape[m_nCurrentShape]->m_pCoordinates[e].x;
	C = m_arrShape[m_nCurrentShape]->m_pCoordinates[b].y * m_arrShape[m_nCurrentShape]->m_pCoordinates[e].x - m_arrShape[m_nCurrentShape]->m_pCoordinates[b].x * m_arrShape[m_nCurrentShape]->m_pCoordinates[e].y;
}

BOOL CChemGraphicsDoc::Between(INT m_nCurrentShape,CPoint m_ptMouse, int i)
{
	int x1, y1, x2, y2;
	const int constDist = 5;
	int b = i,e = i + 1;
	if(i == m_arrShape[m_nCurrentShape]->m_nAngle - 1)
		e = 0;
	
	x2 = max(m_arrShape[m_nCurrentShape]->m_pCoordinates[b].x, m_arrShape[m_nCurrentShape]->m_pCoordinates[e].x) + constDist;
	x1 = min(m_arrShape[m_nCurrentShape]->m_pCoordinates[b].x, m_arrShape[m_nCurrentShape]->m_pCoordinates[e].x);
	y2 = max(m_arrShape[m_nCurrentShape]->m_pCoordinates[b].y, m_arrShape[m_nCurrentShape]->m_pCoordinates[e].y) + constDist;
	y1 = min(m_arrShape[m_nCurrentShape]->m_pCoordinates[b].y, m_arrShape[m_nCurrentShape]->m_pCoordinates[e].y);

	if((m_ptMouse.x <= x2) && (m_ptMouse.x >= x1) && (m_ptMouse.y <= y2) && (m_ptMouse.y >= y1))
		return TRUE;
	return FALSE;
}

void CChemGraphicsDoc::LineDist(float &CDist , float &LDist, float &RDist,INT m_nCurrentShape , CPoint m_ptMouse, int i)
{
	int b = i,e = i + 1;
	CPoint m_pCenter;
	if(i == m_arrShape[m_nCurrentShape]->m_nAngle - 1)
		e = 0;
	
	m_pCenter = ((m_arrShape[m_nCurrentShape]->m_pCoordinates[b].x + m_arrShape[m_nCurrentShape]->m_pCoordinates[e].x) / 2, 
				 (m_arrShape[m_nCurrentShape]->m_pCoordinates[b].y + m_arrShape[m_nCurrentShape]->m_pCoordinates[e].y) / 2);
	CDist = Distance(m_pCenter, m_ptMouse);
	LDist = Distance(m_arrShape[m_nCurrentShape]->m_pCoordinates[b], m_ptMouse);
	RDist = Distance(m_arrShape[m_nCurrentShape]->m_pCoordinates[e], m_ptMouse);
}

float CChemGraphicsDoc::Distance(CPoint a, CPoint b)
{
	return sqrtf((float)(a.x - b.x) * (a.x - b.x) + (a.y - b.y * (a.y - b.y)));
}
//08-10-2014
void CChemGraphicsDoc::DeleteTracker()
{
	CPoint m_pT(0,0);
	m_tracker.m_rect.TopLeft() = m_pT;
	m_tracker.m_rect.BottomRight() = m_pT;
}

//06-10-2014 - BASE SHAPE
void CChemGraphicsDoc::OnDrawArrow()
{
	m_nShapeType = ARROW_T;
}

void CChemGraphicsDoc::OnDrawCircle()
{
	m_nShapeType = CIRCLE_T;
//	m_nShapeType = TRACKER_BITMAP;
}

void CChemGraphicsDoc::OnDrawEclipse()
{
	m_nShapeType = ECLIPSE_T;
}

void CChemGraphicsDoc::OnDrawLine()
{
	m_nShapeType = LINE_T;
}

void CChemGraphicsDoc::OnDrawRectangle()
{
	m_nShapeType = RECTANEGLE_T;
}

void CChemGraphicsDoc::OnDrawSquare()
{
	m_nShapeType = SQUARE_T;
}

//06-10-2014 - BONE
void CChemGraphicsDoc::OnDrawOneBond()
{
	m_nShapeType = ONEBOND_T;
}

void CChemGraphicsDoc::OnDrawDoubleBond()
{
	m_nShapeType = DOUBLEBOND_T;
}

void CChemGraphicsDoc::OnDrawThreeBond()
{
	m_nShapeType = TRIBOND_T;
}

void CChemGraphicsDoc::OnDrawCycloHexane()
{
	m_nShapeType = CYCLOHEXANEBOND_T;
}

void CChemGraphicsDoc::OnDrawCircleHexane()
{
	m_nShapeType = CIRCLEBENZENE_T;
}

void CChemGraphicsDoc::OnDrawBenzene()
{
	m_nShapeType = BENZENBOND_T;
}

void CChemGraphicsDoc::OnDrawCirclePentane()
{
	m_nShapeType = CYCLOPENTANE_T;
}

void CChemGraphicsDoc::OnDrawWedgeBond()
{
	m_nShapeType = WEDGEBOND_T;
}

void CChemGraphicsDoc::OnDrawHashedWedge()
{
	m_nShapeType = HASHEDWEDGE_T;
}

void CChemGraphicsDoc::OnDrawHashBond()
{
	m_nShapeType = HASHBOND_T;
}
//19-10-2014 - add new bond

void CChemGraphicsDoc::OnDrawWhiteHashBond()
{
	m_nShapeType = WHITEHASHBOND_T;
}

void CChemGraphicsDoc::OnDrawUndefineBond()
{
	m_nShapeType = UNDEFINEBOND_T;
}

void CChemGraphicsDoc::OnDrawBoldBond()
{
	m_nShapeType = BOLDBOND_T;
}

void CChemGraphicsDoc::OnDrawDoubleBoldBond()
{
	m_nShapeType = DOUBLEBOLDBOND_T;
}

void CChemGraphicsDoc::OnDrawDoubleArrow()
{
	m_nShapeType = DOUBLEARROW_T;
}

//06-10-2014 - OBITAN
void CChemGraphicsDoc::OnDrawCircleObitan()
{
	m_nShapeType =OBITALCIRCLE_T;
}

void CChemGraphicsDoc::OnDrawBCircleObitan()
{
	m_nShapeType = OBITALBOLD_T;
}


void CChemGraphicsDoc::OnDrawEclipseObitan()
{
	m_nShapeType = OBITALESCLIPSE_T;
}


void CChemGraphicsDoc::OnDrawBEclipseObitan()
{
	m_nShapeType = OBITALESCLIPSEBOLD_T;
}


void CChemGraphicsDoc::OnDrawFunnelObitan()
{
	m_nShapeType = OBITALPIE_T;
}


void CChemGraphicsDoc::OnDrawBFunnelObitan()
{
	m_nShapeType = OBITALPIEBOLD_T;
}

void CChemGraphicsDoc::OnDrawDFunnelObitan()
{
	m_nShapeType = OBITALDOUBLEPIE_T;
}

void CChemGraphicsDoc::OnDrawDBFunnelObitan()
{
	m_nShapeType = OBITALDOUBLEPIEBOLD_T;
}


void CChemGraphicsDoc::OnDrawDFunnelObitan1()
{
	m_nShapeType = OBITALSMFLOWER_T;
}

void CChemGraphicsDoc::OnDrawDBFunnelObitan1()
{
	m_nShapeType = OBITALSMFLOWERBOLD_T;
}

void CChemGraphicsDoc::OnDrawDBFunnelObitan2()
{
	m_nShapeType = OBITALSMFLOWERBOLD1_T;
}

//06-10-2014 - SYMBOL
void CChemGraphicsDoc::OnDrawPlusCircle()
{
	m_nShapeType = PLUSCIRCLE_T;
}

void CChemGraphicsDoc::OnDrawSubCircle()
{
	m_nShapeType = SUBCIRCLE_T;
}

void CChemGraphicsDoc::OnDrawPlus()
{
	m_nShapeType = PLUS_T ;
}

void CChemGraphicsDoc::OnDrawPlusPoint()
{
	m_nShapeType = PLUSPOINT_T;
}

void CChemGraphicsDoc::OnDrawSubPoint()
{
	m_nShapeType = SUBPOINT_T;
}

void CChemGraphicsDoc::OnDrawOnePoint()
{
	m_nShapeType = ONEPOINT_T;
}

void CChemGraphicsDoc::OnDrawDoublePoint()
{
	m_nShapeType = DOUBLEPOINT_T;
}

void CChemGraphicsDoc::OnDrawSub()
{
	m_nShapeType = SUB_T;
}






void CChemGraphicsDoc::OnDialogFont()
{
	// TODO: Add your command handler code here
	CFontDialog dlg;
	LOGFONT lf1;
	if(dlg.DoModal() == IDOK && m_CurrText >= 0)
	{ 
		dlg.GetCurrentFont(&lf1); 
		m_arrShape[m_CurrText]->lf = lf1;
		UpdateAllViews(NULL);
	}
}

POINT CChemGraphicsDoc::GetCaretPosition() 
{
	POINT pt = {0, 0};
	CClientDC dc(NULL); 
//	CFont *ptrFtOld;
	CFont m_ftDraw;
	CString szStr;
	CSize sizeExtent;
//	LOGFONT lf;
	unsigned int nFind = 0;
	if(m_CurrText >= 0)
	{
		/*
		//ASSERT(m_ftDraw.GetSafeHandle()); 
		//ptrFtOld = dc.GetCurrentFont();
		//ptrFtOld->GetLogFont(&m_arrShape[m_CurrText]->lf); 
		//VERIFY(m_ftDraw.CreateFontIndirect(&m_arrShape[m_CurrText]->lf));
		//m_ftDraw.DeleteObject();
		//m_ftDraw.CreateFontIndirect(&m_arrShape[m_CurrText]->lf);
		//ptrFtOld = dc.SelectObject(&m_ftDraw);
		//szStr = m_szText.Left(m_nCaretIndex);
		*/
		CFont font;
		LOGFONT lf;
		int length;
		int i = 0, i1 = 0;
		memset(&lf, 0, sizeof(LOGFONT));       // zero out structure
		lf.lfHeight = 20;                      // request a 12-pixel-height font
		_tcsncpy_s(lf.lfFaceName, LF_FACESIZE, 
		   _T("Time New Romen"), 7);                    // request a face name "Arial"
		VERIFY(font.CreateFontIndirect(&lf));  // create the font 

		// Do something with the font just created...
		CClientDC dc(NULL);
		CFont* def_font = dc.SelectObject(&font);
		szStr = m_arrShape[m_CurrText]->m_szText.Left(m_nCaretIndex); 
		sizeExtent = dc.GetTextExtent(szStr); 
		length = szStr.GetLength();

		if (szStr.Find('_') == -1 && szStr.Find('^') == -1)
		{
			pt.x = m_arrShape[m_CurrText]->m_pBegin.x + sizeExtent.cx;
			pt.y = m_arrShape[m_CurrText]->m_pBegin.y + 5;
		}
		else if(length > 0)
		{
			for(int j = 0; j <= length - 1; j++)
			{
				if(szStr[j] == '_')
					i++;
				if(szStr[j] == '^')
					i1++;
			}
			if(m_arrShape[m_CurrText]->m_szText[length - 1] == '_')
			{
				pt.x = m_arrShape[m_CurrText]->m_pBegin.x + sizeExtent.cx - dc.GetTextExtent(L"_").cx * i - dc.GetTextExtent(L"^").cx * i1;
				pt.y = m_arrShape[m_CurrText]->m_pBegin.y + 10;
			}
			else if (m_arrShape[m_CurrText]->m_szText[length - 1] == '^')
			{
				pt.x = m_arrShape[m_CurrText]->m_pBegin.x + sizeExtent.cx - dc.GetTextExtent(L"_").cx * i - dc.GetTextExtent(L"^").cx * i1;
				pt.y = m_arrShape[m_CurrText]->m_pBegin.y - 5;
			}
			else
			{
				pt.x = m_arrShape[m_CurrText]->m_pBegin.x + sizeExtent.cx - (3 + dc.GetTextExtent(L"_").cx) * i - (3 + dc.GetTextExtent(L"^").cx ) * i1;
				pt.y = m_arrShape[m_CurrText]->m_pBegin.y + 5;
			}
		}

		dc.SelectObject(def_font);
		// Done with the font. Delete the font object.
		font.DeleteObject();
	}
	return pt;
}

void CChemGraphicsDoc::ForwardCaret()
{ 
	m_nCaretIndex++;
	if(m_CurrText >= 0)
		if(m_nCaretIndex > m_arrShape[m_CurrText]->m_szText.GetLength())
		{
			m_nCaretIndex = m_arrShape[m_CurrText]->m_szText.GetLength();
		} 
	GetCGDIView();
	g_ptrView->RedrawCaret();
}

void CChemGraphicsDoc::BackwardCaret() 
{
	m_nCaretIndex--;
	if(m_CurrText >= 0)
		if(m_nCaretIndex < 0) 
		{
			m_nCaretIndex = 0;
		}
	GetCGDIView();
	g_ptrView->RedrawCaret(); 
}

void CChemGraphicsDoc::GetCGDIView()
{
	POSITION pos;
	pos = GetFirstViewPosition();
	do
	{
		g_ptrView=(CChemGraphicsView *)GetNextView(pos);
	}
	while(!g_ptrView->IsKindOf(RUNTIME_CLASS(CChemGraphicsView)));
}

void CChemGraphicsDoc::AddChar(UINT uChar, UINT uRepCnt) 
{ 
	CString szStr;
	UINT i;
	if(m_CurrText >= 0)
	{
	szStr = m_arrShape[m_CurrText]->m_szText;
	m_arrShape[m_CurrText]->m_szText = m_arrShape[m_CurrText]->m_szText.Left(m_nCaretIndex);
	szStr = szStr.Right(szStr.GetLength() - m_nCaretIndex);
	for(i = 0; i < uRepCnt; i++)
		m_arrShape[m_CurrText]->m_szText += (TCHAR)uChar;
	m_arrShape[m_CurrText]->m_szText += szStr; 
	UpdateAllViews(NULL);
	ForwardCaret();
	}
}

void CChemGraphicsDoc::DeleteChar(BOOL bBefore)
{ 
	CString szStr;
	if(m_CurrText >= 0)
		if(bBefore == TRUE) 
		{ 
			if(m_nCaretIndex != 0)
			{
				szStr = m_arrShape[m_CurrText]->m_szText;
				m_arrShape[m_CurrText]->m_szText = m_arrShape[m_CurrText]->m_szText.Left(m_nCaretIndex-1);
				szStr = szStr.Right(szStr.GetLength() - m_nCaretIndex);
				m_arrShape[m_CurrText]->m_szText += szStr; 
				UpdateAllViews(NULL); 
				BackwardCaret();
			}
		}
		else
		{
			if(m_nCaretIndex != m_szText.GetLength())
			{ 
				szStr = m_arrShape[m_CurrText]->m_szText;
				m_arrShape[m_CurrText]->m_szText = m_arrShape[m_CurrText]->m_szText.Left(m_nCaretIndex);
				szStr = szStr.Right(szStr.GetLength()-m_nCaretIndex-1); 
				m_arrShape[m_CurrText]->m_szText += szStr; 
				UpdateAllViews(NULL);
			}
		}
}

void CChemGraphicsDoc::HomeCaret() 
{
	if(m_CurrText >= 0)
	{
		m_nCaretIndex = 0; 
		GetCGDIView();
		g_ptrView->RedrawCaret();
	}
}

void CChemGraphicsDoc::EndCaret()
{
	if(m_CurrText >= 0)
	{
		m_nCaretIndex = m_arrShape[m_CurrText]->m_szText.GetLength();
		GetCGDIView();
		g_ptrView->RedrawCaret(); 
	}
}

void CChemGraphicsDoc::ForwardCaretToBlank()
{
	CString szSub;
	int nFwd;
	if(m_CurrText >= 0)
	{
		szSub = m_arrShape[m_CurrText]->m_szText.Right(m_arrShape[m_CurrText]->m_szText.GetLength() - m_nCaretIndex);
		nFwd = szSub.Find(' ');
		if(nFwd == -1)EndCaret();
		else
		{
			m_nCaretIndex += nFwd+1; 
			GetCGDIView();
			g_ptrView->RedrawCaret();
		}
	}
}
void CChemGraphicsDoc::BackwardCaretToBlank() 
{
	CString szSub; 
	int nBkd;
	if(m_CurrText >= 0)
	{
		szSub = m_arrShape[m_CurrText]->m_szText.Left(m_nCaretIndex-1); 
		nBkd=szSub.ReverseFind(' ');
		if(nBkd == -1)HomeCaret();
		else
		{ 
			m_nCaretIndex -= szSub.GetLength() - nBkd;
			GetCGDIView();
			g_ptrView->RedrawCaret();
		}
	}
}

void CChemGraphicsDoc::SetCaret(CPoint point) 
{
	int nFinalDist;
	int nDist;
	int nIndex;
	int nStrLen;
	int i;
	CString szStr;
	CFont *ptrFtOld;
	CSize sizeExtent;
	CClientDC dc(NULL);
	CFont font;
	LOGFONT lf;

	memset(&lf, 0, sizeof(LOGFONT));       // zero out structure
	lf.lfHeight = 20;                      // request a 12-pixel-height font
	_tcsncpy_s(lf.lfFaceName, LF_FACESIZE, 
		_T("Time New Romen"), 7);          // request a face name "Arial"
	VERIFY(font.CreateFontIndirect(&lf));  // create the font 
	
	if(m_CurrText >= 0)
	{
		ptrFtOld = dc.SelectObject(&font);
		nStrLen = m_arrShape[m_CurrText]->m_szText.GetLength(); 
		nIndex = 0;
		nFinalDist = point.x;
		for(i = 1; i <= nStrLen; i++) 
		{ 
			szStr = m_arrShape[m_CurrText]->m_szText.Left(i);
			sizeExtent = dc.GetTextExtent(szStr);
			nDist = abs(point.x - sizeExtent.cx);
			if(nDist < nFinalDist)
			{
				nFinalDist = nDist; 
				nIndex = i;
			} 
		}
		dc.SelectObject(ptrFtOld);
		m_nCaretIndex = nIndex;
		GetCGDIView();
		g_ptrView->RedrawCaret();
		dc.SelectObject(ptrFtOld);
		font.DeleteObject();
	}
}

void CChemGraphicsDoc::OnUpdateDialogFont(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here

}


void CChemGraphicsDoc::OnEditPaste()
{
	// TODO: Add your command handler code here
	// TODO: Add your command handler code here
	HANDLE hMem; 
	LPSTR lpStr;
	CString szStr;
	if(m_CurrText >= 0)
	{
		GetCGDIView();
		g_ptrView->OpenClipboard();
		hMem=::GetClipboardData(CF_TEXT);
		ASSERT(hMem != NULL); 
		lpStr=(LPSTR)::GlobalLock(hMem);
		ASSERT(lpStr); 
		szStr = lpStr;
		m_nCaretIndex += szStr.GetLength(); 
		::GlobalUnlock(hMem); 
		::CloseClipboard();
		m_arrShape[m_CurrText]->m_szText = m_arrShape[m_CurrText]->m_szText.Left(m_nCaretIndex - szStr.GetLength()) 
			+ szStr 
			+ m_arrShape[m_CurrText]->m_szText.Right(m_arrShape[m_CurrText]->m_szText.GetLength() - m_nCaretIndex + 1);
		UpdateAllViews(NULL);
		GetCGDIView();
		g_ptrView->RedrawCaret();
	}
}


void CChemGraphicsDoc::OnUpdateEditPaste(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));

}


void CChemGraphicsDoc::OnText()
{
	m_nShapeType = TEXT_T;
	// TODO: Add your command handler code here
}


void CChemGraphicsDoc::OnUpdateArrow(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == ARROW_T);
}



void CChemGraphicsDoc::OnUpdateCircle(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == CIRCLE_T);
}



void CChemGraphicsDoc::OnUpdateEclipse(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
		pCmdUI->SetCheck(m_nShapeType == ECLIPSE_T);
}



void CChemGraphicsDoc::OnUpdateLine(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == LINE_T);
}




void CChemGraphicsDoc::OnUpdateRectangle1(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == RECTANEGLE_T);
}




void CChemGraphicsDoc::OnUpdateSquare(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == SQUARE_T);
}



void CChemGraphicsDoc::OnUpdateOnebond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == ONEBOND_T);
}



void CChemGraphicsDoc::OnUpdateDoublebond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == DOUBLEBOND_T);
}




void CChemGraphicsDoc::OnUpdateThreebone(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
		pCmdUI->SetCheck(m_nShapeType == TRIBOND_T);
}

void CChemGraphicsDoc::OnUpdateCyclohexane(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == CYCLOHEXANEBOND_T);
}




void CChemGraphicsDoc::OnUpdateCirclehexane(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == CIRCLEBENZENE_T);
}

void CChemGraphicsDoc::OnUpdateBenzene(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == BENZENBOND_T);
}

void CChemGraphicsDoc::OnUpdateCirclepentane(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == CYCLOPENTANE_T);
}

void CChemGraphicsDoc::OnUpdateWedgebond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == WEDGEBOND_T);
}

void CChemGraphicsDoc::OnUpdateHashedwaged(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == HASHEDWEDGE_T);
}

void CChemGraphicsDoc::OnUpdateHashbond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == HASHBOND_T);
}

void CChemGraphicsDoc::OnUpdateWhitewedgebond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == WHITEHASHBOND_T);
}

void CChemGraphicsDoc::OnUpdateUndefinebond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == UNDEFINEBOND_T);
}

void CChemGraphicsDoc::OnUpdateBoldbond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == BOLDBOND_T);
}

void CChemGraphicsDoc::OnUpdateDoubleboldbond(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == DOUBLEBOLDBOND_T);
}


void CChemGraphicsDoc::OnUpdateDoublearrow(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == DOUBLEARROW_T);
}



void CChemGraphicsDoc::OnUpdateBcircleobitan(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == OBITALBOLD_T);
}


void CChemGraphicsDoc::OnUpdateEclipseobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALESCLIPSE_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateBeclipseobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALESCLIPSEBOLD_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateFunnelobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALPIE_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateBfunnelobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALPIEBOLD_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateDfunnelobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALDOUBLEPIE_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateDbfunnelobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALDOUBLEPIEBOLD_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateDfunnelobitan1(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALSMFLOWER_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateDbfunnelobitan1(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALSMFLOWERBOLD_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateDbfunnelobitan2(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALSMFLOWERBOLD1_T);
	// TODO: Add your command update UI handler code here
}



void CChemGraphicsDoc::OnUpdatePluscircle(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == PLUSCIRCLE_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateSubcircle(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == SUBCIRCLE_T);
	// TODO: Add your command update UI handler code here
}



void CChemGraphicsDoc::OnUpdatePlus(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == PLUS_T);
	// TODO: Add your command update UI handler code here
}



void CChemGraphicsDoc::OnUpdatePluspoint(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == PLUSPOINT_T);
	// TODO: Add your command update UI handler code here
}



void CChemGraphicsDoc::OnUpdateSubpoint(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == SUBPOINT_T);
	// TODO: Add your command update UI handler code here
}



void CChemGraphicsDoc::OnUpdateOnepoint(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == ONEPOINT_T);
	// TODO: Add your command update UI handler code here
}



void CChemGraphicsDoc::OnUpdateDoublepoint(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == DOUBLEPOINT_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateSub(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == SUB_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnUpdateCircleobitan(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_nShapeType == OBITALCIRCLE_T);
	// TODO: Add your command update UI handler code here
}


void CChemGraphicsDoc::OnCyclopropane()
{
	m_nShapeType = CYCLOPROPANE_T;
	// TODO: Add your command handler code here
}


void CChemGraphicsDoc::OnUpdateCyclopropane(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == CYCLOPROPANE_T);
}



void CChemGraphicsDoc::OnCyloputan()
{
	// TODO: Add your command handler code here
	m_nShapeType = CYCLOPUTANE_T;
}


void CChemGraphicsDoc::OnUpdateCyloputan(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nShapeType == CYCLOPUTANE_T);
}


void CChemGraphicsDoc::OnButtonSelectarea()
{
	// TODO: Add your command handler code here
	m_nShapeType = TRACKER_BITMAP;
}

